package com.wen.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageRowBounds;
import com.wen.mapper.RoleMapper;
import com.wen.mapper.UserMapper;
import com.wen.mapper.UserRoleMapper;
import com.wen.pojo.CurrentUser;
import com.wen.pojo.User;
import com.wen.pojo.UserRole;
import com.wen.service.RoleService;
import com.wen.service.UserService;
import com.wen.utils.Md5Util;
import com.wen.utils.PageBean;
import com.wen.utils.Result;
import com.wen.utils.ThreadLocalUtil;
import dto.AssignRoleDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public User findUserByCode(String userCode) {
        return userMapper.findUserByCode(userCode);
    }

    @Override
    public PageBean<User> allUserList(Integer pageNum, Integer pageSize, User user) {
        PageBean<User> pb = new PageBean<>();
        PageHelper.startPage(pageNum,pageSize);
        List<User> as = userMapper.allUserList(user);
        //转换
        Page<User> p = (Page<User>) as;

        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public Boolean addUser(User user) {
        User u = userMapper.findUserByCode(user.getUserCode());
        if(u == null){
            CurrentUser currentUser =ThreadLocalUtil.get();
            int userId = currentUser.getUserId();
            String upwd = Md5Util.getMD5String(user.getUserPwd());
            user.setUserPwd(upwd);
            user.setCreateBy(userId);
            userMapper.addUser(user);
            return true;
        }else{
            return false;
        }

    }

    @Override
    public Result setStateById(User user) {
        int i = userMapper.setStateById(user.getUserId(), user.getUserState());
        if(i>0){
            return Result.ok(200,"修改成功",null);
        }else{
            return Result.fail(1,"修改失败");
        }
    }
    @Override
    public Result assignRole(AssignRoleDto assignRoleDto) {
        userRoleMapper.delUserRole(assignRoleDto.getUserId());
        List<String> roleCheckList = assignRoleDto.getRoleCheckList();
        for (String item : roleCheckList){
            Integer roleId = roleMapper.findByName(item);
            userRoleMapper.addUserRole(new UserRole(null,roleId,assignRoleDto.getUserId()));
        }
        return Result.ok(200,"修改成功",null);
    }

    @Override
    public Result deleteUserList(int[] userList) {
        int i = userMapper.deleteUserList(userList);
        if(i>0){
            return Result.ok(200,"删除成功",null);
        }else{
            return Result.fail(1,"删除失败");
        }
    }

    @Override
    public Result updUserInfo(User user) {
        CurrentUser currentUser = ThreadLocalUtil.get();
        int userId = currentUser.getUserId();
        user.setCreateBy(userId);
        int i =  userMapper.updUserInfo(user);
        if(i>0){
            return Result.ok(200,"修改成功",null);
        }else{
            return Result.fail(1,"修改失败");
        }
    }

    @Override
    public Result updatePwd(Integer userId) {
        String pwd = "123456";
        String md5Pwd = Md5Util.getMD5String(pwd);
        int i = userMapper.updatePwd(userId,md5Pwd);
        if(i>0){
            return Result.ok(200,"重置密码成功",null);
        }else{
            return Result.fail(1,"重置密码失败");
        }
    }


}
